package com.itheima.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @program: itheima-service-gateway
 * @description: 检查是否有token
 * @author: zhanghz001
 * @create: 2021-06-03 15:49
 **/
@Configuration
@Slf4j
public class ZhzAutoTokenFileter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token1");
        if (StringUtils.isEmpty(token)) {
            log.error("没有token");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        ;
        
        return chain.filter(exchange);
        
    }
    
    @Override
    public int getOrder() {
        return 1;
    }
}
